#include "socket_functions.h"

// 初始化Socket环境
socket_t init_socket() {
    socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == INVALID_SOCKET_VALUE) {
        perror("socket failed");
        return INVALID_SOCKET_VALUE;
    }
    
    return sock;
}

// 创建Socket服务器
socket_t create_server(int port) {
    socket_t server_socket = init_socket();
    if (server_socket == INVALID_SOCKET_VALUE) {
        return INVALID_SOCKET_VALUE;
    }
    
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(port);
    
    // 绑定Socket
    if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR_VALUE) {
        perror("bind failed");
        close_socket(server_socket);
        return INVALID_SOCKET_VALUE;
    }
    
    // 开始监听
    if (listen(server_socket, 5) == SOCKET_ERROR_VALUE) {
        perror("listen failed");
        close_socket(server_socket);
        return INVALID_SOCKET_VALUE;
    }
    
    printf("Server started successfully, listening on port: %d\n", port);
    
    return server_socket;
}

// 接受客户端连接
socket_t accept_client(socket_t server_socket) {
    struct sockaddr_in client_addr;
    socklen_t client_addr_len = sizeof(client_addr);
    
    socket_t client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len);
    if (client_socket == INVALID_SOCKET_VALUE) {
        perror("accept failed");
        return INVALID_SOCKET_VALUE;
    }
    
    char client_ip[INET_ADDRSTRLEN];
    inet_ntop(AF_INET, &(client_addr.sin_addr), client_ip, INET_ADDRSTRLEN);
    printf("Client connected successfully: %s:%d\n", client_ip, ntohs(client_addr.sin_port));
    
    return client_socket;
}

// 连接到服务器
socket_t connect_to_server(const char* server_ip, int port) {
    socket_t client_socket = init_socket();
    if (client_socket == INVALID_SOCKET_VALUE) {
        return INVALID_SOCKET_VALUE;
    }
    
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    
    if (inet_pton(AF_INET, server_ip, &(server_addr.sin_addr)) <= 0) {
        printf("Invalid server IP address\n");
        close_socket(client_socket);
        return INVALID_SOCKET_VALUE;
    }
    
    if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR_VALUE) {
        perror("Failed to connect to server");
        close_socket(client_socket);
        return INVALID_SOCKET_VALUE;
    }
    
    printf("Successfully connected to server: %s:%d\n", server_ip, port);
    
    return client_socket;
}

// 发送数据
int send_data(socket_t socket, const char* data, int data_len) {
    int sent_bytes = send(socket, data, data_len, 0);
    if (sent_bytes == SOCKET_ERROR_VALUE) {
        perror("Failed to send data");
    }
    
    return sent_bytes;
}

// 接收数据
int recv_data(socket_t socket, char* buffer, int buffer_len) {
    int recv_bytes = recv(socket, buffer, buffer_len - 1, 0);
    if (recv_bytes == SOCKET_ERROR_VALUE) {
        perror("Failed to receive data");
    } else if (recv_bytes > 0) {
        buffer[recv_bytes] = '\0';  // 添加字符串结束符
    }
    
    return recv_bytes;
}

// 关闭Socket
void close_socket(socket_t socket) {
    close(socket);
}

// 清理Socket环境
void cleanup_socket() {
    // Linux环境下不需要特殊的清理操作
}